package com.op.crm.webapp.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;

import com.op.crm.persist.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class PermissionInterceptor extends AbstractInterceptor {
	
	public final static String NO_PERMISSION = "no_permission";

	/**
	 * 
	 */
	private static final long serialVersionUID = -3936772203980270629L;

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		ActionContext actionContext = arg0.getInvocationContext();   
		HttpServletRequest request= (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST); 
		request.setCharacterEncoding("UTF-8");

	    Map session = actionContext.getSession();  
	    User user = (User) session.get("crm_user");
	    
    	String uri = request.getRequestURI();
    	/*
    	if (user.getRoles() == null || user.getRoles().size() == 0) {
    		return NO_PERMISSION;
    	}
    	
    	for (Role r : user.getRoles()) {
    		if (r.getActions() != null && r.getActions().size() > 0) {
    			for (Action a : r.getActions()) {
    				if (uri.indexOf(a.getAction()) != -1) {
    					return arg0.invoke();
    				}
    			}
    		}
    	}
    	
    	return NO_PERMISSION;*/
    	
    	return arg0.invoke();
	    

	}


}
